package com.example.menglinhe.yuejian308.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.menglinhe.yuejian308.R;
import com.example.menglinhe.yuejian308.bean.LoginBean;
import com.example.menglinhe.yuejian308.model.LoginModel;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LoginActivity extends AppCompatActivity {
    private Button lg;
    private Button rg;

    private EditText user,pwd;

    private String phone,psswd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        lg = (Button)findViewById(R.id.lg);
        rg = (Button)findViewById(R.id.rg);
        user = (EditText) findViewById(R.id.user);
        pwd = (EditText) findViewById(R.id.pwd);



        lg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                  Showlist();
            }
        });
    }
    void Showlist(){
       String phone=user.getText().toString();
        String psswd=pwd.getText().toString();
        LoginModel orderModel = new LoginModel();
        Call call = orderModel.checkLogin(phone,psswd);
        call.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                if (response.isSuccessful()){
                    LoginBean loginBean = response.body();
                    if (0!=loginBean.getUserid()){
                        Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                        startActivity(intent);
                        finish();
                        Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
                    }
                    else {
                        Toast.makeText(LoginActivity.this,"登录失败",Toast.LENGTH_SHORT).show();
                    }
                }
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                Toast.makeText(LoginActivity.this,"访问服务器超时", Toast.LENGTH_SHORT).show();
            }
        });
    }


}